﻿Imports Microsoft.WindowsAzure
Imports Microsoft.WindowsAzure.StorageClient
Public Class PruebaDataSource
    Private Shared storageAccount As CloudStorageAccount
    Private context As PruebaDataContext
    Public Sub New()
        Me.context = New PruebaDataContext(storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials)
        Me.context.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(1))
    End Sub
    Shared Sub New()
        storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString")
        CloudTableClient.CreateTablesFromModel(GetType(PruebaDataContext), storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials)
    End Sub
    Public Function GetPruebaEntries() As IEnumerable(Of PruebaEntry)
        Dim results = From g In Me.context.PruebaEntry _
                      Where g.PartitionKey = DateTime.UtcNow.ToString("MMddyyyy") _
                      Select g
        Return results
    End Function
    Public Sub AddPreubaEntry(ByVal newItem As PruebaEntry)
        Me.context.AddObject("PruebaEntry", newItem)
        Me.context.SaveChanges()
    End Sub

    Public Sub UpdateImageThumbnail(ByVal partitionKey As String, ByVal rowKey As String, ByVal thumbUrl As String)
        Dim results = From g In Me.context.PruebaEntry _
                      Where g.PartitionKey = partitionKey AndAlso g.RowKey = rowKey _
                      Select g
        Dim entry = results.FirstOrDefault()
        entry.ThumbnailUrl = thumbUrl
        Me.context.UpdateObject(entry)
        Me.context.SaveChanges()
    End Sub
End Class
